#include "ScoreSystem.h"

void ScoreSystem::SetupDefaults()
{
	// this is used if we don't define any special value for the level
	m_scores.m_current_level=0;
	for( int i = 0; i < MAX_LEVEL_COUNT; ++i ) {
		m_scores.highscores[i] = 5;
	}
	
	m_scores.highscores[0] = 1;
    m_scores.highscores[1] = 7;
    m_scores.highscores[2] = 2;
    m_scores.highscores[3] = 20;
    m_scores.highscores[4] = 20;
    m_scores.highscores[5] = 20;
    m_scores.highscores[6] = 20;
    m_scores.highscores[7] = 20;
    m_scores.highscores[8] = 20;
    m_scores.highscores[9] = 20;
    m_scores.highscores[10] = 20;
    m_scores.highscores[11] = 20;
    m_scores.highscores[12] = 30;
}

void ScoreStorage::Serialise()
{
    // save the current level 
    IwSerialiseInt32( m_current_level );

    for( int i = 0; i < MAX_LEVEL_COUNT; ++i ) {
        IwSerialiseInt32( highscores[i] );
    }
}

void ScoreSystem::Load()
{
    m_scores.m_current_level=0;

    if( IwSerialiseExists( "scores.bin", false ) == false ) {
        // setup the default scores for all levels
        SetupDefaults();
        
        // and save it
        Save();
    } else {
        IwSerialiseOpen("scores.bin", true);

        // serialise the object in
        m_scores.Serialise();

        // close the file
        IwSerialiseClose();
    }
}

void ScoreSystem::Save()
{
    // setup the default scores for all levels
    IwSerialiseOpen("scores.bin", false);

    // now serialise the current scores
    m_scores.Serialise();

    // close the file
    IwSerialiseClose();
}

void ScoreSystem::StartNew( int level )
{
    m_scores.m_current_level = level;
    m_current_score = 0;
}

void ScoreSystem::FinishedLevel()
{
	++ m_scores.m_current_level;
	Save();
}

void ScoreSystem::ShipUnloadingFinished( const Ship& ship )
{
    ++m_current_score;
}

int ScoreSystem::GetCurrentScore() const
{
    return m_current_score;
}

int ScoreSystem::GetCurrentHighscore() const
{
    return m_scores.highscores[m_scores.m_current_level];
}

int ScoreSystem::GetCurrentLevel() const
{
	return m_scores.m_current_level;
}